// import React from 'react';
// import { useForm, SubmitHandler } from 'react-hook-form';
// import axios from 'axios';
// import './taskform.css';

// // 定义表单输入的接口
// interface TaskFormInputs {
//   title: string;
//   description: string;
//   assignee: string;
//   dueDate: string;
//   tags: string;
//   project_id: string;
// }

// // 定义表单组件的属性接口
// interface TaskFormProps {
//   onTaskCreated: () => void;
// }

// const TaskForm: React.FC<TaskFormProps> = ({ onTaskCreated }) => {
//   const { register, handleSubmit, reset } = useForm<TaskFormInputs>();

//   // 处理表单提交的异步函数
//   const onSubmit: SubmitHandler<TaskFormInputs> = async (data) => {
//     try {
//       await axios.post('http://localhost:3000/cyw/addtasks', data);
//       onTaskCreated(); // 通知父组件任务已创建
//       reset(); // 重置表单
//     } catch (error) {
//       console.error('Error creating task:', error);
//     }
//   };

//   return (
//     <form onSubmit={handleSubmit(onSubmit)}>
//       <input {...register('title', { required: true })} placeholder="标题" className='title' />
//       <br />
//       <textarea {...register('description')} placeholder="描述" className='description' />
//       <br />
//       <input {...register('assignee')} placeholder="负责人" className='assignee' />
//       <br />
//       <input type="date" {...register('dueDate')} placeholder="截止日期" className='dueDate' />
//       <br />
//       <input {...register('tags')} placeholder="标签" className='tags' />
//       <br />
//       <input {...register('project_id')} placeholder="Project ID" className='project_id' />
//       <br />
//       <button type="submit" className='submit'>新建</button>
//     </form>
//   );
// };

// export default TaskForm;


import React from'react';
import { useForm, SubmitHandler } from'react-hook-form';
import { useNavigate } from'react-router-dom'; // 引入useNavigate用于导航
import axios from 'axios';
import './taskform.css';

// 定义表单输入的接口
interface TaskFormInputs {
  title: string;
  description: string;
  assignee: string;
  dueDate: string;
  tags: string;
  project_id: string;
}

const TaskForm: React.FC = () => {
  const { register, handleSubmit, reset } = useForm<TaskFormInputs>();
  const navigate = useNavigate(); // 获取导航函数

  // 处理表单提交的异步函数
  const onSubmit: SubmitHandler<TaskFormInputs> = async (data) => {
    try {
      await axios.post('http://localhost:3000/cyw/addtasks', data);
      navigate('/cywindex'); // 新建成功后跳回根路径
      reset(); 
    } catch (error) {
      console.error('Error creating task:', error);
    }
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)} className='task-form'>
      <input {...register('title', { required: true })} placeholder="请添加标题" className='title' />
      <br />
      <textarea {...register('description')} placeholder="请写下内容吧" className='description'/>
      <br />
      <input {...register('assignee')} placeholder="负责人" className='assignee' />
      
      
      <input {...register('tags')} placeholder="标签" className='tags' />
     
      <input {...register('project_id')} placeholder="Project ID" className='project_id' />
      <br />
      <input type="date" {...register('dueDate')} placeholder="截止日期" className='dueDate' />
      
      <button type="submit" className='submit'>新建</button>
    </form>
  );
};

export default TaskForm;